package com.sfx.shortLink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sfx.shortLink.dao.entity.LinkAccessStatsDO;
import com.sfx.shortLink.dto.LinkAccessStatsDTO;
import com.sfx.shortLink.dto.req.ShortLinkGroupStatsReqDTO;
import com.sfx.shortLink.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ILinkAccessStatsMapper extends BaseMapper<LinkAccessStatsDO> {

    // 唯一索引 : full_short_url, gid, date,weekday, hour
    @Insert("INSERT INTO t_link_access_stats(full_short_url, gid, date, pv, uv, uip, hour, weekday, create_time, update_time, del_flag)" +
            "VALUES( #{linkStats.fullShortUrl}, #{linkStats.gid}, #{linkStats.date}, #{linkStats.pv},#{linkStats.uv}," +
            "#{linkStats.uip}, #{linkStats.hour}, #{linkStats.weekday}, NOW(), NOW(), 0) ON DUPLICATE KEY UPDATE " +
            "pv = pv +  #{linkStats.pv}, " +
            "uv = uv + #{linkStats.uv}, " +
            "uip = uip + #{linkStats.uip};")
    void shortLinkStatus(@Param("linkStats") LinkAccessStatsDO linkStats);

//    /**
//     * 基础访问详情(指定日期返回每天的 uv pv uip)
//     *
//     * @param param
//     * @return
//     */
//    @Select("SELECT date ," +
//            "SUM(pv) as pv ," +
//            "SUM(uv) as uv, " +
//            "SUM(uip) as uip " +
//            "FROM t_link_access_stats " +
//            "where full_short_url = #{param.fullShortUrl} and gid = #{param.gid} and date between #{param.startDate} and #{param.endDate} " +
//            "group by full_short_url, gid, date ")
//    List<LinkAccessStatsDO> listStatsByShortLink(@Param("param") ShortLinkStatsReqDTO param);

    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    date, " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, date;")
    List<LinkAccessStatsDTO> listStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);


    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    date, " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "     gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY gid, date;")
    List<LinkAccessStatsDTO> listStatsByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO param);

    /**
     * 基础访问详情(指定日期返回每天的 uv pv uip)
     *
     * @param param
     * @return
     */
    @Select("SELECT " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid;")
    LinkAccessStatsDTO statsByShortLink(@Param("param") ShortLinkStatsReqDTO param);//ShortLinkGroupStatsReqDTO


    /**
     * 基础访问详情(指定日期返回每天的 uv pv uip) 分组
     *
     * @param param
     * @return
     */
    @Select("SELECT " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "     gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "     gid;")
    LinkAccessStatsDTO statsByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO param);


    @Select("SELECT hour,SUM(pv) as pv " +
            "FROM t_link_access_stats " +
            "WHERE full_short_url = #{param.fullShortUrl} AND gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by full_short_url,gid,hour;"
    )
    List<LinkAccessStatsDTO> listHourStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    @Select("SELECT hour,SUM(pv) as pv " +
            "FROM t_link_access_stats " +
            "WHERE gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by gid,hour;"
    )
    List<LinkAccessStatsDTO> listHourStatsByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);


    @Select("SELECT weekday,SUM(pv) as pv " +
            "FROM t_link_access_stats " +
            "WHERE full_short_url = #{param.fullShortUrl} AND gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by full_short_url,gid,weekday;"
    )
    List<LinkAccessStatsDTO> listWeekdayStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);


    @Select("SELECT weekday,SUM(pv) as pv " +
            "FROM t_link_access_stats " +
            "WHERE gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by gid,weekday;"
    )
    List<LinkAccessStatsDTO> listWeekdayStatsByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);
}